/**
 * 
 */
package com.sidom.movieIndexer.mock;

import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.client.RestTemplate;

import com.sidom.movieIndexer.ConstantsForTesting;
import com.sidom.movieIndexer.domain.file.MovieFile;
import com.sidom.movieIndexer.factory.MovieFactory;
import com.sidom.movieIndexer.factory.MovieFactory.TestMovie;
import com.sidom.movieIndexer.util.TestUtil;

/**
 * Mocks the call to google search.
 * Returns a json file on the classpath.
 * To create a new mock json file.
 * - Create a file under the src/test/resources/movies directory (with extension avi).
 * - Add it to TestMovie, TestMovieFile and TestGoogleSearchResult
 * - Set the debug level to TRACE
 * - Run test MovieIndexerRealIntegrationTest
 * - Get the Json output from the console
 * - Format the Json format: http://jsonformatter.curiousconcept.com/
 * - Create the json file and paste the json content in it.
 * 
 * @author Siebe
 */
public class GoogleSearchMock {
	public static final String JSON_EQUILIBRIUM 			= "equilibrium.json";
	public static final String JSON_SWORDFISH 				= "swordFish.json";
	public static final String JSON_GONE_IN_60_SECONDS 		= "goneIn60Seconds.json";
	public static final String JSON_YOUTUBE 				= "youtube.json";
	public static final String JSON_BOX_OFFICE	 			= "boxOffice.json";
	public static final String JSON_BOURNE_IDENTITY 		= "bourneIdentity.json";
	public static final String JSON_NO_MOVIE_WITH_THIS_NAME = "noMovieWithThisName.json";
	public static final String JSON_THE_ISLAND 				= "theIsland.json";
	public static final String JSON_TRANSPORTER 			= "transporter.json";
	public static final String JSON_MATRIX 					= "matrix.json";
	public static final String JSON_THE_ITALIAN_JOB 		= "theItalianJob.json";
	public static final String JSON_THE_ITALIAN_JOB_CORRECT	= "theItalianJobCorrect.json";
	public static final String SEARCH_PREFIX				= "IMDB ";
	
	/**
	 * Create a mock for {@link MovieFile#getCleanedFileName()}s in the map
	 * @param movieAndJson
	 *            map with Key the {@link MovieFile#getCleanedFileName()} and value the
	 *            expected JSon output
	 * @param restTemplate The {@link RestTemplate}
	 */
	public static void createMockForMovies(Map<String, String> movieAndJson, RestTemplate restTemplate) {
		for (String cleanedUpMovieName : movieAndJson.keySet()) {
			Map<String, String> searchParameters = new HashMap<String, String>();
			searchParameters.put("googleSearchversion", "1.0");
			searchParameters.put("resultSize", "small");
			searchParameters.put("queryString", SEARCH_PREFIX + cleanedUpMovieName);
			when(
					restTemplate
							.getForObject(
									"http://ajax.googleapis.com/ajax/services/search/web?v={googleSearchversion}&rsz={resultSize}&q={queryString}",
									String.class, searchParameters))
					.thenReturn(movieAndJson.get(cleanedUpMovieName));
		}
	}
	
	/**
	 * Create a mock response for all the movies with the avi extension.
	 * @param restTemplate The {@link RestTemplate}
	 */
	public static void createMockForAviMovies(RestTemplate restTemplate) {
		Map<String, String> moviesAndJson = new HashMap<String, String>();
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.BOURNE_IDENTITY_CD1).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_BOURNE_IDENTITY, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.THE_MATRIX).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_MATRIX, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.NO_MOVIE_WITH_THIS_NAME).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_NO_MOVIE_WITH_THIS_NAME, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.THE_ISLAND).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_THE_ISLAND, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.SWORDFISH_CD1).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_SWORDFISH, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.EQUILIBRIUM_CD1).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_EQUILIBRIUM, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.YOUTUBE).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_YOUTUBE, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.BOX_OFFICE).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_BOX_OFFICE, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.THE_MTRX).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_MATRIX, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.THE_ITALIAN_JOB).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_THE_ITALIAN_JOB, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		createMockForMovies(moviesAndJson, restTemplate);
	}
	
	/**
	 * Create a mock response for all the movies with the mp4 extension.
	 * @param restTemplate The {@link RestTemplate}
	 */
	public static void createMockForMp4Movies(RestTemplate restTemplate) {
		Map<String, String> moviesAndJson = new HashMap<String, String>();
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.GONE_IN_60_SECONDS).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_GONE_IN_60_SECONDS, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		moviesAndJson.put(MovieFactory.createMovie(TestMovie.TRANSPORTER).getMovieFile().getCleanedFileName(), TestUtil.getFileFromClassPath(GoogleSearchMock.JSON_TRANSPORTER, ConstantsForTesting.TEST_RESOURCE_DIRECORY_JSON));
		createMockForMovies(moviesAndJson, restTemplate);
	}
}
